// ==UserScript==
// @name           Remove Google_Alimama_Baidu_Sogou Ads
// @namespace      http://by-igotit.com
// @include        *
// @exclude        http://by-igotit.com/*
// ==/UserScript==

(function() {

  var VERSION = "1.03.2010.05.05";

  var p = {
    Google: [/googleads\.g\.doubleclick\.net/],
    Alimama: [/z\.alimama\.com/],
    Baidu: [/cpro\.baidu\.com/],
    Sogou: [/inte\.sogou\.com/]
  };

  function setAdCount(site) {
    GM_setValue("AdRemovedCount_"+site, GM_getValue("AdRemovedCount_"+site, 0) + 1);
  }

  function notify(){
    var divstyle = [
      "width: 400px !important",
      "height: 20px !important",
      "background-color: red !important",
      "color: white !important",
      "font-family: arial, serif !important",
      "font-weight: bold !important",
      "position: fixed !important",
      "right: 0px !important",
      "bottom: 0px !important",
      "text-align: center !important",
      "z-index: 100 !important;"
    ];
    var divtimeout = 4000;
    var newdiv = getNotifierDiv();

    var str = "";
    for (var key in p){
      str += key+" : " + GM_getValue("AdRemovedCount_"+key, 0) + "&nbsp;&nbsp;";
    }
    newdiv.innerHTML = str;
    newdiv.setAttribute("style", divstyle.join("; "));

    abody = window.top.document.getElementsByTagName("body")[0];
    abody.insertBefore(newdiv, abody.firstChild);
    setTimeout(function(){newdiv.parentNode.removeChild(newdiv);}, divtimeout);
  }

  function getNotifierDiv(){
    var adiv = document.getElementById("ad-removed-notifier");
    if(adiv)
      return adiv;
    else {
      adiv = document.createElement("div");
      adiv.setAttribute("id", "ad-removed-notifier");
      adiv.setAttribute("class", "ad-removed-notifier");
      return adiv;
    }
  }
  
  function modifyTag(args) {
    var s = document.evaluate(
        args.path,
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);
    
    for(var i = 0; i < s.snapshotLength; i++){
      args.handler(i, s);
    }
  }
  
  modifyTag({
    path : "//iframe",
    handler : function(i, s) {
      var node = s.snapshotItem(i);
      var adsrc = node.getAttribute("src");
      for (var key in p){
        for(var i in p[key]){
          if(p[key][i].test(adsrc)){
            node.parentNode.removeChild(node);
            setAdCount(key);
            notify();
          }
        }
      }
    }
  });
  
})();

